/**
 * 付款界面
 * author:zhangjlt
 * date:2017年3月16日13:30:42
 */
;
define(function(require, exports, module) {
	var wechat = require("wechat");
	var qr = require("lib/qrcode");
	var jsbarcode = require("lib/JsBarcode.all");
	var payTo = {
		data: {},
		ret:{},
		/**
		 * bindEvent 页面事件
		 */
		bindEvent: function() {
			//返回
			$("#return_payTo").unbind().on("click", function() {
				window.location.href = "#home";
			});
		},
		/**
		 * 进行页面路由监听配置
		 * 
		 */
		bindRout: function() {
			//公共页面监听路由
			Path.map("#home").to(function() {
				clearInterval(payTo.ret);
				//返回首页
				common.loadPage({
					pageUrl: "html/index.html",
					reload: true,
					from: "left"
				});
			});
			Path.listen();
		},
		/**
		 * 页面数据加载
		 */
		initData: function() {
			var width = $(".pay_bg").width();
			var left =(width-150)/2;
			$("#QRcode_payto").css("left",left);
			var headImgUrl = window.localStorage.getItem("headImgUrl");
			$("#headimg_payto").attr("src", headImgUrl);
			var wxUserInfostr = window.localStorage.getItem("WxUserInfo");
			if(wxUserInfostr) {

				var wxUserInfoObj = JSON.parse(wxUserInfostr);
				if(wxUserInfoObj.status == "0") {
					$("#name_payto").text(wxUserInfoObj.data.name);
					$("#credit_payto").text(wxUserInfoObj.data.myPoints);
					$("#account_payto").text(wxUserInfoObj.data.balance);
					$("#level_payto").text(wxUserInfoObj.data.currentLevel);
				}
			} else {
				common.showTip({
					content: "调用接口出错"
				});
			}
			payTo.getqrcode();
			var count = 180;
			payTo.ret = setInterval(function() {
				--count;
				if(count == 0) {
					payTo.getqrcode();
				} 
			},1000);
			
		},
		getqrcode: function() {
			var openId = window.localStorage.getItem("openId");
			var appId = window.localStorage.getItem("appId");
			var param = {
				"openId": openId,
				"appId": appId

			};
			common.loadData({
				url: "/wxmember/service/getPaymentCode",
				param: param,
				successCallBack: function(json) {
					if(json.status == "0") {
						$("#QRcode_payto").html("");
						var qrcode = new QRCode(document.getElementById("QRcode_payto"), {
							width: 150,
							height: 150
						});
						qrcode.makeCode(json.data.paymentCode);
						//			JsBarcode("#code_payto","123");
						$("#code_payto").JsBarcode(json.data.paymentCode);

					}
				}, //回调函数
				dataSource: "getShareDetail",
				cache: false //标识是不加载全部
			});
		},
		/**
		 * each page init mothed
		 */
		initialize: function() {
			//动态加载数据
			this.initData();
			//事件绑定
			this.bindEvent();
			//路由绑定
			this.bindRout();
		}
	};

	module.exports = payTo;
});